//
//  PedomViewController.m
//  BaChat
//
//  Created by dllo on 16/6/28.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "PedomViewController.h"

#import "YNPedometerManager.h"
#import "UIView+Twinkle.h"

@interface PedomViewController ()


@property (retain, nonatomic)  UILabel *StepsLabel;
@property (retain, nonatomic)  UILabel *DistanceLabel;
@property (retain, nonatomic) UILabel *CadenceLabel;
@property (retain, nonatomic) UILabel *StepsText;
@property (retain, nonatomic) UILabel *DistanceText;
@property (retain, nonatomic) UILabel *CadenceTaxt;

@end

@implementation PedomViewController
- (void)dealloc {
    [_StepsLabel release];
    [_DistanceLabel release];
    [_CadenceLabel release];
    [_StepsText release];
    [_DistanceText release];
    [_CadenceTaxt release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = RGBAColor(30, 5, 50);
    self.title = @"运 动";
    [self createData];
    
    [self.StepsLabel twinkle];
    [self.DistanceLabel twinkle];
    [self.CadenceLabel twinkle];
   
    
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbar_back_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    
    __block PedomViewController *pedometerVC = self;
    
    if ([YNPedometerManager isStepCountingAvailable]) {
        [[YNPedometerManager shared]
         startPedometerUpdatesTodayWithHandler:^(YNPedometerData *pedometerData,NSError *error) {
             if (!error) {
                 
                 pedometerVC.StepsLabel.text = [NSString stringWithFormat:@"%@ 步",pedometerData.numberOfSteps];
                 
                
                 
                 float distance = [pedometerData.distance floatValue ] / 1000;
                 
                 pedometerVC.DistanceLabel.text = [NSString stringWithFormat:@"%.2f",distance];
                 
                 float cadence = [pedometerData.numberOfSteps floatValue] / 25;
                 pedometerVC.CadenceLabel.text = [NSString stringWithFormat:@"%.1f",cadence];
                 
             }
             
         }];
    } else {
        
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"此设备仅支持iPhone5s及以上设备!" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }

    
}


-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void) createData{
    
    self.StepsLabel = [[UILabel alloc]init];
//    self.StepsLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.StepsLabel];
    
    self.StepsLabel.textAlignment = NSTextAlignmentCenter;
    self.StepsLabel.font = [UIFont systemFontOfSize:70];
    self.StepsLabel.textColor = [UIColor yellowColor];
    
    [self.StepsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view).multipliedBy(0.7);
        make.height.mas_equalTo(self.view).multipliedBy(0.2);
        make.top.mas_equalTo(self.view).offset(100);
        make.center.mas_equalTo(self.view);
        
    }];
    
    
    
    self.StepsText = [[UILabel alloc]initWithFrame:CGRectMake(90, -10, 100, 30)];
//    self.StepsText.backgroundColor = [UIColor yellowColor];
    self.StepsText.text = @"今日步数";
    self.StepsText.textColor = RGBAColor(255, 228, 181);
    [self.StepsLabel addSubview:self.StepsText];

    self.DistanceLabel = [[UILabel alloc]init];
//    self.DistanceLabel.backgroundColor = [UIColor yellowColor];
    self.DistanceLabel.textAlignment = NSTextAlignmentCenter;
    self.DistanceLabel.font = [UIFont systemFontOfSize:40];
    self.DistanceLabel.textColor = [UIColor yellowColor];
    [self.view addSubview:self.DistanceLabel];
    
    self.CadenceLabel = [[UILabel alloc]init];
//  self.CadenceLabel.backgroundColor = [UIColor yellowColor];
     self.CadenceLabel.textAlignment = NSTextAlignmentCenter;
    self.CadenceLabel.font = [UIFont systemFontOfSize:40];
    self.CadenceLabel.textColor = [UIColor yellowColor];
    [self.view addSubview:self.CadenceLabel];
    
    [self.DistanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.width.mas_equalTo(self.view).multipliedBy(0.4);
        make.height.mas_equalTo(self.view).multipliedBy(0.1);
        make.top.mas_equalTo(self.StepsLabel.mas_bottom).offset(80);
        make.right.mas_equalTo(self.CadenceLabel).offset(20);
        make.left.mas_equalTo(self.view).offset(20);
        
    }];
    
    [self.CadenceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(self.view).multipliedBy(0.1);
        make.top.mas_equalTo(self.StepsLabel.mas_bottom).offset(80);
        make.left.mas_equalTo(self.DistanceLabel.mas_right).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
        
    }];
    
    self.DistanceText = [[UILabel alloc]initWithFrame:CGRectMake(30, self.DistanceLabel.frame.size.height + self.DistanceLabel.frame.origin.y - 15, 150, 30)];
//    self.DistanceText.backgroundColor = [UIColor yellowColor];
    self.DistanceText.text = @"今日距离(km)";
    self.DistanceText.textColor = RGBAColor(255, 228, 181);
    [self.DistanceLabel addSubview:self.DistanceText];

    self.CadenceTaxt = [[UILabel alloc]initWithFrame:CGRectMake(220, 285, 150, 30)];
//    self.CadenceTaxt.backgroundColor = [UIColor yellowColor];
    self.CadenceTaxt.text = @"卡路里(kcal)";
    self.CadenceTaxt.textColor = RGBAColor(255, 228, 181);
//    self.CadenceTaxt.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.CadenceTaxt];
    
    
    
    [_StepsLabel release];
    [_StepsText release];
    [_DistanceLabel release];
    [_DistanceText release];
    [_CadenceLabel release];
    [_CadenceTaxt release];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
